package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.entity.Dept;
import org.example.xsda.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 院系控制器
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/page")
    public Result<PageResult<Dept>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String keyword
    ) {
        PageResult<Dept> pageResult = deptService.page(current, size, keyword);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result<List<Dept>> list() {
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<Dept> getById(@PathVariable Long id) {
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @SaCheckRole("admin")
    @PostMapping
    public Result<Void> add(@Validated @RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    @SaCheckRole("admin")
    @PutMapping
    public Result<Void> update(@Validated @RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }

    @SaCheckRole("admin")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        deptService.delete(id);
        return Result.success();
    }
}

